package com.allen.retrofit2_rxjava2.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.base.BaseActivity;
import com.allen.retrofit2_rxjava2.bean.Login;
import com.allen.retrofit2_rxjava2.module.MainContract;
import com.allen.retrofit2_rxjava2.presenter.MainPresenter;
import com.allen.retrofit2_rxjava2.utils.MainUtil;
import com.allen.retrofit2_rxjava2.view.HookViewClickUtil;
import com.allen.retrofit2_rxjava2.view.selectImage.FeedbackActivity;
import com.allen.retrofit2_rxjava2.view.selectimageview.PhotoActivity;
import com.allen.retrofit2_rxjava2.view.selectimageview.utils.CropUtil;
import com.allen.retrofit2_rxjava2.web.WebViewActivity;
import com.ieyelf.service.net.type.Jpeg;
import com.ieyelf.service.util.FileUtils;

import java.io.File;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements MainContract.View {
    private MainPresenter presenter;

    @BindView(R.id.btn_login)
    Button btn_login;

    @BindView(R.id.btn_banner)
    Button btn_banner;

    @BindView(R.id.btn_zixun)
    Button btn_zixun;

    @BindView(R.id.et_code)
    EditText et_code;

    @BindView(R.id.iv_code)
    ImageView iv_code;

    @BindView(R.id.tv_content)
    TextView tv_content;

    @BindView(R.id.vs)
    ViewStub vs;//

    @BindView(R.id.tv_double_click)
    TextView tv_double_click;//

    @BindView(R.id.head_image)
    ImageView head_image;//

    @BindView(R.id.tip)
    TextView tip;//


    private final static int REQEST_CODE_HEAD = 1005;// 获取截取头像返回的值

    @Override //viewId的初始化都在init结束后
    public void init() {
        setContentView(R.layout.activity_main);
        presenter = new MainPresenter(this, this);
        presenter.setTokenTest(); //测试设置token
        //inflate()方式加载布局,只能加载一次

    }

    private void spannableString() {
        final String linkWord1 = "《用户服务协议》";
        final String linkWord2 = "《隐私协议》";
        String word = "点击注册表示已阅读并同意 " + linkWord1 + "和" + linkWord2;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(word);
        int index1 = word.indexOf(linkWord1);
        int index2 = word.indexOf(linkWord2);
        spannableStringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, linkWord1, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.RED);    //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }
        }, index1, index1 + linkWord1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableStringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, linkWord2, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.GREEN);       //设置文件颜色
                ds.setUnderlineText(false);      //设置下划线
            }
        }, index2, index2 + linkWord2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tip.setTextSize(20);
        tip.setText(spannableStringBuilder);
        tip.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void data() {
        if (null == vs) {
            MainUtil.log("1111111111");
            vs = (ViewStub) findViewById(R.id.vs);
            vs.inflate();
            vs.setVisibility(View.VISIBLE);
        } else {
            vs.setVisibility(View.GONE);
            MainUtil.log("2222222222");
        }
        spannableString();
    }

    @Override
    public void listener() {
        super.listener();
        tv_double_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("防止重复点击响应的click/可对实践拦截");
            }
        });
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                HookViewClickUtil.hookView(tv_double_click);
            }
        });
    }

    @OnClick({R.id.btn_login, R.id.btn_banner, R.id.btn_zixun, R.id.iv_code, R.id.tv_web, R.id.tv_dragdelete, R.id.image_select, R.id.images_select})
    public void clickBtn(View view) {
        String code = et_code.getText().toString().trim();
        switch (view.getId()) {
            case R.id.btn_login:
//                presenter.userLogin("allen234", "allen234", code);
                MainUtil.log("1111111111");
                showToast();
                startActivity(new Intent(this, FragmentActivity.class));
                break;
            case R.id.btn_banner:
                presenter.getBannerData();
                break;
            case R.id.btn_zixun:
                presenter.getLiveData();
                break;
            case R.id.iv_code:
                presenter.getCode();
                break;
            case R.id.tv_web://web测试页面
                startActivity(new Intent(this, WebViewActivity.class));
                break;
            case R.id.tv_dragdelete:
                startActivity(new Intent(this, DragDeleteActivity.class));
                break;
            case R.id.image_select:
                startActivityForResult(new Intent(this, PhotoActivity.class), REQEST_CODE_HEAD);
                break;
            case R.id.images_select:
                startActivity(new Intent(this, FeedbackActivity.class));
                break;
        }
    }

    /**
     * 查看头像返回结果
     */
    private void checkHeaderImage() {
        String filename = CropUtil.getRealFilePath();
        Jpeg jpeg = FileUtils.loadJpegFromTestFile(filename);
        if (jpeg == null) {
            return;
        }
        File headFile = new File(filename);
        Bitmap headBitmap = null;
        if (headFile != null && headFile.exists())
            headBitmap = BitmapFactory.decodeFile(filename);
        if (null != headBitmap) {
            head_image.setImageBitmap(headBitmap);
        } else {
            head_image.setImageResource(R.mipmap.defult_head);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == REQEST_CODE_HEAD) {
            checkHeaderImage();
        }
    }

    @Override
    public void setContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            tv_content.setText(content);
        }
    }


    @Override
    public void setLogin(Login content) {

    }

    @Override
    public void setCode(Bitmap value) {
        //通过imageview，设置图片
        iv_code.setImageBitmap(value);
    }


    public void showToast() {
        showToast("点我啊");
    }

    public void test() {
    }
}
